#!/usr/bin/python
# coding=utf-8

import urllib
import urlparse
import re
import datetime
from helpers import read_url, decode

patterns = {
    'shortname': re.compile('<H1>Klub Parlamentarny ([^<]+)<br>', re.I|re.M),
    'mail': re.compile('<a href="mailto:([^"]+)">', re.I|re.M),
    'www': re.compile('<a href="(http://[^"]+)">http://[^<]+</a>', re.I|re.M),
    'member':  re.compile('<LI><A HREF="([^"]+)">([^<]+)</A>', re.I|re.M),
}

def join_url(base, url):
    if not url:
        return None
    return urlparse.urljoin(base, url)

def get_shortname(soup):
    try:
        match = patterns['shortname'].search(soup)
        return match.group(1)
    except:
        return None

def get_mail(soup):
    match = patterns['mail'].search(soup)
    return match.group(1)

def get_www(soup):
    match = patterns['www'].search(soup)
    if not match:
        return None
    return match.group(1)

def get_members(soup, path):
    members = patterns['member'].findall(soup)
    mlist = []
    for member in members:
        mlist.append({
            'name': member[1],
            'url': join_url(path, member[0]),
        })
    return mlist

def get_party(path):
    source = read_url(path)
    party = {
        'shortname': decode(get_shortname(source)),
        'email': get_mail(source),
        'www': get_www(source),
        'members': get_members(source, path),
    }
    return party


def main():
    path = 'http://www.senat.gov.pl/k7/senat/kluby/pis.htm'
    party = get_party(path)
    print party
    

if __name__ == "__main__":
    main()
